(function() {
    Nokia.TopBar = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {};

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

            instance.registerData("topbar");

            instance._create();

			instance.registerVibrationOn([ ]);

			instance.fireCallback('create');
        },

		_create: function() {
			var instance = this;
			var options = instance.options;
			var Styles = instance.Styles;

			instance.wrapper = Nokia.dom.parseHTML('<div class="' + [Styles.topBar, Styles.clearFix, Styles.uiWidgetHeader].join(' ') + '"></div>');

			instance.topContainer = Nokia.dom.parseHTML('<div class="' + [Styles.topContainer].join(' ') + '"></div>');

			Nokia.dom.append(instance.wrapper, instance.topContainer);
			Nokia.dom.append(instance.element, instance.wrapper);

			instance.setItems(options.items);

		},

		setItems: function(items) {
			var instance = this;
			var Styles = instance.Styles;

			instance.topContainer.empty();

			jQuery.each(items, function(i, item) {
				var navigable = item.navigable === false ? false : true;

				var itemElement;

				if (navigable) {
					itemElement = Nokia.dom.parseHTML('<a href="javascript:void(0);"></a>');
				} else {
					itemElement = Nokia.dom.parseHTML('<span></span>');
				}

				itemElement.addClass([Styles.topBarItem, Styles.topBarIcon, item.classname].join(' '));
				itemElement.html(item.label);

				if (item.callback) {
					itemElement.unbind('click.topbar');
					itemElement.bind('click.topbar', function(event) {

						if (item.pressedStateClass) {

							if (!item.keepPressed) {
								itemElement.addClass(item.pressedStateClass);

								setTimeout(function() {
									itemElement.removeClass(item.pressedStateClass);

									item.callback.apply(item, [event]);
								}, 100);

							} else {
								itemElement.toggleClass(item.pressedStateClass);

								item.callback.apply(item, [event]);
							}

						} else {
							item.callback.apply(item, [event]);
						}

					});

					Nokia.util.bindEnter(itemElement, function() {
						itemElement.trigger('click.topbar');
					});
				}

				Nokia.dom.append(instance.topContainer, itemElement);
			});
		},

		Styles: {
			topBar: 'nokia-topbar',
			topBarItem: 'nokia-topbar-item',
			topBarIcon: 'nokia-topbar-icon',
			topContainer: 'nokia-topbar-container',
			clearFix: 'ui-helper-clearfix',
			uiStateActive: 'ui-state-active',
			uiWidgetHeader: 'ui-widget-header'
		}
    });
})();